package app.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import app.domain.Feedback;
import app.domain.Worker;
import app.service.FeedbackService;
import app.utils.DateUtil;

@Controller
public class FeedbackController {
	public static Logger logger=LogManager.getLogger(FeedbackController.class);
	@Autowired
	private FeedbackService feedbackService;
	


	@RequestMapping(value = "/commitFeedback",method = RequestMethod.POST)
	public String commitFeedback(Feedback feedback,HttpServletRequest request){
		System.out.println(feedback.toString());
		feedbackService.addFeedback(feedback);
		request.setAttribute("message", "<script>alert(\"谢谢您的反馈，我们会及时处理!\")</script>");
		return "index";
	}
	
	@RequestMapping(value = "/feedbackList",method = RequestMethod.GET)
	public String showFeedbackList(HttpServletRequest request){
		List<Feedback> feedbacks = feedbackService.showFeedbackList();
		request.setAttribute("feedbacks", feedbacks);
		return "admin/feedbackList";
	}
	
	@RequestMapping(value = "/deleteFeedback/{id}")
	public String deleteWorkById(@PathVariable("id") Integer id,HttpServletRequest request){
		feedbackService.deleteWorkById(id);
		
		List<Feedback> feedbacks = feedbackService.showFeedbackList();
		request.setAttribute("message", "<script>alert(\"删除成功\")</script>");
		request.setAttribute("feedbacks", feedbacks);
		return "admin/feedbackList";
	}
	
	@RequestMapping(value = "/replyFeedback",method = RequestMethod.POST)
	public String replyFeedback(Integer id,String content,HttpServletRequest request){
		System.out.println(id+content);
		
		List<Feedback> feedbacks = feedbackService.showFeedbackList();
		request.setAttribute("message", "<script>alert(\"回复成功\")</script>");
		request.setAttribute("feedbacks", feedbacks);
		return "admin/feedbackList";
	}
	@RequestMapping(value = "/findFeedback",method = RequestMethod.POST)
	public String replyFeedback(String preDate,String nextDate,String confident,HttpServletRequest request) throws Exception{
		System.out.println(preDate+nextDate+confident);
		System.out.println(preDate==null);
		System.out.println(nextDate==null);
		System.out.println(confident==null);

		Date dateStart = DateUtil.StringToDate(preDate, "yyyy-MM-dd");

		Date dateEnd = DateUtil.StringToDate(nextDate, "yyyy-MM-dd");

		
		List<Feedback> feedbacks = feedbackService.findFeedbackList(dateStart,dateEnd,confident);
		//request.setAttribute("message", "<script>alert(\"回复成功\")</script>");
		request.setAttribute("feedbacks", feedbacks);
		return "admin/feedbackList";
	}
}
